package com.example.demo.Entity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;

public class FSFolder {
    public void setName(StringBuffer name) {
        this.name = name;
    }

    public void setTime(StringBuffer time) {
        this.time = time;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public void setFilelist(LinkedList<FSFile> filelist) {
        this.filelist = filelist;
    }

    public void setChildFolderList(LinkedList<FSFolder> childFolderList) {
        this.childFolderList = childFolderList;
    }

    public StringBuffer getName() {
        return name;
    }

    public StringBuffer getTime() {
        return time;
    }

    public long getSize() {
        return size;
    }

    public LinkedList<FSFile> getFilelist() {
        return filelist;
    }

    public LinkedList<FSFolder> getChildFolderList() {
        return childFolderList;
    }

    private StringBuffer name;
    private StringBuffer time;
    private long size;
    private LinkedList<FSFile> filelist;
    private LinkedList<FSFolder> childFolderList;

    //添加树节点
    public void addFolderTreeNode(FSFolder newFolder){
        if(null == this.childFolderList){
            LinkedList<FSFolder> folderTree = new LinkedList<>();
            folderTree.addLast(newFolder);
            this.setChildFolderList(folderTree);
        }
        else {
            this.childFolderList.addLast(newFolder);
        }
    }


    //遍历树测试
    public void travel(FSFolder travelRootFolder,int n){
        if(travelRootFolder.childFolderList != null){
            for(FSFolder travelLeafFolder:travelRootFolder.childFolderList){
                //函数
                n++;
                for(int i = 0;i<=n;i++){
                    System.out.print("    ");
                }
                System.out.println(travelLeafFolder.getName().toString()+"\n");
                travelLeafFolder.travel(travelLeafFolder,n);
                n--;
            }
        }
    }

    //获取特定节点的文件夹子树
    public LinkedList<FSFolder> getFolderTree(FSFolder rootFolder){
        LinkedList<FSFolder> folderTree = new LinkedList<>();
        if (rootFolder.childFolderList != null){
            for(FSFolder childFolder : rootFolder.childFolderList){
                folderTree.addLast(childFolder);
            }
        }
        else {
            folderTree = null;
        }
        if(folderTree != null){
            for(FSFolder folderTreeChild : folderTree){
                folderTreeChild.setChildFolderList(folderTreeChild.getFolderTree(folderTreeChild));
            }
        }
        return folderTree;
    }



    //文件系统复制、粘贴、剪切、新建文件夹、上传文件、查询文件、重命名


    //新建文件夹
    public FSFolder createNewFolder(StringBuffer newFolderName){
        FSFolder newFolder = new FSFolder();
        if(newFolderName == null){
            newFolder.setName(new StringBuffer("new folder"));
        }
        else {
            newFolder.setName(newFolderName);
        }
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        newFolder.setTime(new StringBuffer(format.format(new Date())));
        this.setTime(new StringBuffer(format.format(new Date())));
        this.addFolderTreeNode(newFolder);
        return newFolder;
    }

    //添加文件
    public void addFSFile(FSFile newFile){
        if(null == this.filelist){
            LinkedList<FSFile> files = new LinkedList<>();
            files.addLast(newFile);
            this.setFilelist(files);
        }
        else {
            this.filelist.addLast(newFile);
        }
    }

    //复制文件
   public FSFile copyFSFile(FSFile sourceFile){
        StringBuffer copyName = new StringBuffer(sourceFile.getName().toString());
        StringBuffer copyTime = new StringBuffer(sourceFile.getTime().toString());
        String copyType = new String(sourceFile.getType());
        boolean copyRecycle = sourceFile.isRecycle();
        long copySize = sourceFile.getSize();
        boolean copyShare = sourceFile.isShare();
        FSFile copyFile = new FSFile(copyName.toString(),copyTime.toString(),copySize,copyShare,copyType,copyRecycle);
        return copyFile;
    }

    //复制文件夹本身
    public FSFolder copyFSFolder(){
        FSFolder copyFolder = new FSFolder();
        copyFolder.setName(this.getName());
        copyFolder.setSize(this.getSize());
        copyFolder.setTime(this.getTime());
        for(FSFile sourceFile : this.filelist){
            copyFolder.filelist.addLast(this.copyFSFile(sourceFile));
        }
        for(FSFolder sourceFolder : this.childFolderList){
            copyFolder.childFolderList.addLast(sourceFolder.copyFSFolder());
        }
        return copyFolder;
    }

    //复制文件夹参数版（备用）
    /*
    public FSFolder copyFSFolder(FSFolder sourceFolder){
        FSFolder copyFolder = new FSFolder();
        copyFolder.setName(sourceFolder.getName());
        copyFolder.setSize(sourceFolder.getSize());
        copyFolder.setTime(sourceFolder.getTime());
        for(FSFile sourceFile : sourceFolder.filelist){
            copyFolder.filelist.addLast(sourceFolder.copyFSFile(sourceFile));
        }
        for(FSFolder source : sourceFolder.childFolderList){
            copyFolder.childFolderList.addLast(source.copyFSFolder());
        }
        return copyFolder;
    }
    */

    //粘贴文件
    public void pasteFSFile(FSFile sourceFile){
        if(null == this.filelist){
            LinkedList<FSFile> fileList = new LinkedList<>();
            fileList.addLast(sourceFile);
            this.setFilelist(fileList);
        }
        else {
            this.filelist.addLast(sourceFile);
        }
        this.setSize(this.getSize()+sourceFile.getSize());
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        this.setTime(new StringBuffer(format.format(new Date())));
    }

    //粘贴文件夹
    public void pasteFSFolder(FSFolder sourceFolder){
        this.addFolderTreeNode(sourceFolder);
        this.setSize(this.getSize()+sourceFolder.getSize());
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        this.setTime(new StringBuffer(format.format(new Date())));
    }

    //剪切文件（调用此函数不可回退,须和粘贴文件函数一起调用）
    public FSFile cutFSFile(FSFile sourceFile){
        this.setSize(this.getSize()-sourceFile.getSize());
        //待补充检测异常
        this.filelist.remove(sourceFile);
        return sourceFile;
    }

    //剪切文件夹（调用此函数不可回退,须和粘贴文件夹函数一起调用）
    public FSFolder cutFSFolder(FSFolder sourceFolder){
        this.setSize(this.getSize()-sourceFolder.getSize());
        this.childFolderList.remove(sourceFolder);
        return sourceFolder;
    }

    //
    //文件重命名
    public void renameFSFile(FSFile targetFile,String newName){
        //待补充检测异常
        StringBuffer targetName = new StringBuffer(newName);
        int index = this.filelist.indexOf(targetFile);
        this.filelist.get(index).setName(targetName);
    }

    //子文件夹重命名
    public void renameFSFolder(FSFolder targetFolder,String newName){
        //待补充检测异常
        StringBuffer targetName = new StringBuffer(newName);
        int index = this.childFolderList.indexOf(targetFolder);
        this.childFolderList.get(index).setName(targetName);
    }

    //文件夹自身重命名
    public void renameFSFolderSelf(String newName){
        StringBuffer targetName = new StringBuffer(newName);
        this.setName(targetName);
    }

    //根据文件名从文件列表获取文件
    public FSFile getFSFileByName(String name){
        for(FSFile file : this.getFilelist()){
            if(file.getName().toString().equals(name)){
                return file;
            }
        }
        return null;
    }


    //提供文件夹名获取文件夹
    public FSFolder getFolderByName(LinkedList<FSFolder> folderTree,String name){
        for(FSFolder treeNode : folderTree){
            if(treeNode.getName().toString().equals(name)){
                return treeNode;
            }
        }
        return null;
    }

    //根据路径获取文件
    public FSFile getFSFileByPath(String filePath){
        int startIndex = 0;
        int endIndex = 0;
        String folderName;
        String fileName;
        FSFile file = null;
        FSFolder rootFolder = this;
        FSFolder childFolder = null;
        while(endIndex != -1){
            startIndex = filePath.indexOf('/',startIndex)+1;
            endIndex = filePath.indexOf('/',startIndex);
            if(endIndex != -1) {
                folderName = filePath.substring(startIndex, endIndex);
                startIndex = endIndex;
                childFolder = rootFolder.getFolderByName(rootFolder.getChildFolderList(),folderName);
                rootFolder = childFolder;
            }
            else {
                fileName = filePath.substring(startIndex);
                file = rootFolder.getFSFileByName(fileName);
                break;
            }
        }
        return file;
    }

    //根据路径获取文件夹
    public FSFolder getFSFolderByPath(String folderPath){
        int startIndex = 0;
        int endIndex = 0;
        String folderName;
        FSFolder rootFolder = this;
        FSFolder childFolder = null;
        while(endIndex != -1){
            startIndex = folderPath.indexOf('/',startIndex)+1;
            endIndex = folderPath.indexOf('/',startIndex);
            if(endIndex != -1) {
                folderName = folderPath.substring(startIndex, endIndex);
                startIndex = endIndex;
                childFolder = rootFolder.getFolderByName(rootFolder.getChildFolderList(),folderName);
                rootFolder = childFolder;
            }
            else {
                folderName = folderPath.substring(startIndex);
                childFolder = rootFolder.getFolderByName(rootFolder.getChildFolderList(),folderName);
                break;
            }
        }
        return childFolder;
    }

    //根据路径添加文件
    public void addByPath(String path,String type,long size){
        int startIndex = 0;
        int endIndex = 0;
        String folderName;
        String fileName;
        FSFolder rootFolder = this;
        FSFolder childFolder;
        while(endIndex != -1){
            startIndex = path.indexOf('/',startIndex)+1;
            endIndex = path.indexOf('/',startIndex);
            if(endIndex != -1) {
                folderName = path.substring(startIndex, endIndex);
                startIndex = endIndex;
                if(rootFolder.childFolderList == null){
                    LinkedList<FSFolder> folderTree = new LinkedList<>();
                    rootFolder.setChildFolderList(folderTree);
                }
                childFolder = rootFolder.getFolderByName(rootFolder.childFolderList,folderName);
                if(childFolder == null){
                    childFolder = rootFolder.createNewFolder(new StringBuffer(folderName));
                }
                rootFolder = childFolder;
            }
            else {
                fileName = path.substring(startIndex);
                FSFile newFile = new FSFile(fileName,new Date().toString(),size,false,type,false);
                rootFolder.addFSFile(newFile);
                break;
            }

        }

    }

    //显示回收站文件
    public void showRecycleFiles(ArrayList<RecycleFile> arrayList,StringBuffer path){
        path.append("/");
        if(this.getFilelist() != null){
            for(FSFile file : this.getFilelist()){
                if(file.isRecycle()){
                    RecycleFile recycleFile = new RecycleFile(file.getName().toString(),file.getTime().toString(),file.getSize(),file.getType(),path.toString());
                    arrayList.add(recycleFile);
                }
            }
        }
        if (this.getChildFolderList() != null){
            for(FSFolder childFolder : this.getChildFolderList()){
                path.append(childFolder.getName());
                childFolder.showRecycleFiles(arrayList,path);
                path.deleteCharAt(path.length());
                path.delete(path.lastIndexOf("/"),path.length());
            }
        }
    }

}
